package com.maserb.monitor.web.schoolConduct;

import com.maserb.monitor.common.*;
import com.maserb.monitor.entity.JwOrgEntity;
import com.maserb.monitor.entity.UserEntity;
import com.maserb.monitor.entity.jwQuestion.*;
import com.maserb.monitor.entity.schoolConduct.SchoolConductItemEntity;
import com.maserb.monitor.entity.schoolConduct.StudentConductRecordEntity;
import com.maserb.monitor.enums.ConductRecordStatusEnum;
import com.maserb.monitor.service.author.AuthorService;
import com.maserb.monitor.service.author_manage.UserService;
import com.maserb.monitor.service.jwEducation.JwOrgService;
import com.maserb.monitor.service.schoolConduct.SchoolConductItemService;
import com.maserb.monitor.service.schoolConduct.StudentConductRecordService;
import com.maserb.monitor.service.schoolStudent.SchoolStudentParentService;
import com.maserb.monitor.service.schoolStudent.SchoolStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 操行记录
 */
@RestController
@RequestMapping("/schoolConductRecord")
public class SchoolConductRecordController {

    @Autowired
    AuthorService authorService;

    @Autowired
    UserService userService;

    @Autowired
    SchoolStudentParentService parentService;

    @Autowired
    StudentConductRecordService studentConductRecordService;

    @Autowired
    SchoolStudentService schoolStudentService;

    @Autowired
    SchoolConductItemService schoolConductItemService;

    @Autowired
    JwOrgService orgService;

    @RequestMapping(value = {"/", "/index"})
    public ModelAndView getIndex(final HttpServletRequest request) {
        ModelAndView view = new ModelAndView();
        HttpSession session = request.getSession(true);
        UserEntity user = (UserEntity) session.getAttribute("user");
        String userId = user.getId();
        List<JwOrgEntity> clazzList = new ArrayList<JwOrgEntity>();
        List<SchoolConductItemEntity> itemList = new ArrayList<SchoolConductItemEntity>();
        if(null!=user){
            clazzList = orgService.findByUser(user);
            if(null!=user.getOrgId() && !"".equals(user.getOrgId())){
                itemList = schoolConductItemService.findBySchoolId(user.getOrgId());
            }
        }
        view.addObject("userId",userId);
        view.setViewName("school_conduct/record_index");
        view.addObject("clazzList",clazzList);
        view.addObject("itemList",itemList);
        return view;
    }

    /**
     * 学生操行记录
     * @param studentId
     * @return
     */
    @RequestMapping("/doListConductRecordList")
    @ResponseBody
    public PageResponseResult<StudentConductRecordEntity> doListConductRecordList(PageRequestParam page, String studentId) {
        return AjaxUtils.processPage(page, new Func_T1<Pageable, Page<StudentConductRecordEntity>>() {
            @Override
            public Page<StudentConductRecordEntity> invoke(Pageable var1) throws Exception {
                Page<StudentConductRecordEntity> page = studentConductRecordService.findAll(var1);
                /*for(StudentConductRecordEntity entity:page.getContent()){
                    if(!StringUtils.isEmptyOrWhitespace(entity.getCreateUser())){
                        UserEntity user = new UserEntity();
                        try{
                            user = userService.findOne(entity.getCreateUser());
                        }catch(Exception e){
                            e.printStackTrace();
                        }
                        if(null!=user){
                            entity.setCreateUser(user.getRealName());
                        }
                    }
                }*/
                return page;
            }
        });
    }

    @RequestMapping("/doList")
    @ResponseBody
    public PageResponseResult<StudentConductRecordEntity> doList(PageRequestParam page,String userId, String name,String studentId,String clazzId,String itemId,String startTime,String endTime) {
        Sort.Order codeOrder = new Sort.Order(Sort.Direction.DESC, "createTime");
        List<Sort.Order> orderList = new ArrayList<Sort.Order>();
        orderList.add(codeOrder);
        Sort sort = new Sort(orderList);
        return AjaxUtils.processPage(page,sort, new Func_T1<PageRequest, Page<StudentConductRecordEntity>>() {
            @Override
            public Page<StudentConductRecordEntity> invoke(PageRequest var1) throws Exception {
                Page<StudentConductRecordEntity> page = studentConductRecordService.findAll(var1,userId,name,studentId,clazzId,itemId,startTime,endTime);
                for(StudentConductRecordEntity entity:page.getContent()){
                    if(null!=entity.getStatus() && !"".equals(entity.getStatus())){
                        entity.setStatus(ConductRecordStatusEnum.getNameByValue(Integer.parseInt(entity.getStatus())));
                    }
                }
                return page;
            }
        });
    }

    //查看操行记录
    @RequestMapping("/getView/{id}")
    public ModelAndView getViewQuestionFinished(final HttpServletRequest request,@PathVariable final String id) {
        ModelAndView result = new ModelAndView();
        HttpSession session = request.getSession(true);
        UserEntity user = (UserEntity) session.getAttribute("user");
        String userId = user.getId();
        List<JwOrgEntity> clazzList = new ArrayList<JwOrgEntity>();
        List<SchoolConductItemEntity> itemList = new ArrayList<SchoolConductItemEntity>();
        if(null!=user){
            clazzList = orgService.findByUser(user);
            if(null!=user.getOrgId() && !"".equals(user.getOrgId())){
                itemList = schoolConductItemService.findBySchoolId(user.getOrgId());
            }
        }
        ConductRecordStatusEnum[]statusEnums = ConductRecordStatusEnum.values();
        StudentConductRecordEntity entity = studentConductRecordService.findOne(id);
        result.addObject("itemList",itemList);
        result.addObject("statusEnums",statusEnums);
        result.setViewName("school_conduct/student_record_view");
        result.addObject("entity", entity);
        return result;
    }

    /**
     * 审核通过
     * @param keys
     * @return
     */
    @RequestMapping("/doReview")
    @ResponseBody
    public AjaxResult<Boolean> doReview(@RequestParam(value = "keys[]") ArrayList<String> keys)
    {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                for(int i =0;i<keys.size();i++){
                    StudentConductRecordEntity entity = studentConductRecordService.findOne(keys.get(i));
                    if(null!=entity && !StringUtils.isEmptyOrWhitespace(entity.getStatus()) && entity.getStatus().equals("0")){
                        studentConductRecordService.doReview(entity);
                    }else{
                        throw new Exception("请选择未审核的记录");
                    }
                }
                return true;
            }
        });
    }

    /**
     * 审核不通过
     * @param keys
     * @return
     */
    @RequestMapping("/doNotReview")
    @ResponseBody
    public AjaxResult<Boolean> doNotReview(@RequestParam(value = "keys[]") ArrayList<String> keys)
    {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                for(int i =0;i<keys.size();i++){
                    StudentConductRecordEntity entity = studentConductRecordService.findOne(keys.get(i));
                    if(null!=entity && !StringUtils.isEmptyOrWhitespace(entity.getStatus()) && entity.getStatus().equals("0")){
                        studentConductRecordService.doNotReview(entity);
                    }else{
                        throw new Exception("请选择未审核的记录");
                    }
                }
                return true;
            }
        });
    }

}
